package bean;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import Model.HoaDon;
import Model.SanPham;
import Util.DAO;
import Util.HoaDonDataModel;
import Util.SanPhamDataModel;
@ManagedBean
@SessionScoped
public class BeanHoaDon implements Serializable{
	private HoaDonDataModel hddm;
	private List<HoaDon> dshd = new ArrayList<HoaDon>();
	private HoaDon selectedHoaDon;
	private SanPhamDataModel spdm;
	private SanPham selectedSanPham;
	
	public void setSelectedSanPham(SanPham selectedSanPham) {
		this.selectedSanPham = selectedSanPham;
	}
	public SanPham getSelectedSanPham() {
		return selectedSanPham;
	}
	public void setSpdm(SanPhamDataModel spdm) {
		this.spdm = spdm;
	}
	public SanPhamDataModel getSpdm() {
		return spdm;
	}
	public HoaDonDataModel getHddm() {
		return hddm;
	}
	public void setHddm(HoaDonDataModel hddm) {
		this.hddm = hddm;
	}
	public HoaDon getSelectedHoaDon() {
		return selectedHoaDon;
	}
	public void setSelectedHoaDon(HoaDon selectedHoaDon) {
		this.selectedHoaDon = selectedHoaDon;
	}
	public BeanHoaDon() {
		dshd = DAO.laydanhsachhoadon();
	hddm = new HoaDonDataModel(dshd);
	}
	
	public void suahoadon(HoaDon hd){
		DAO.capnhathoadon(hd);
	}
	
	public String xoahoadon(HoaDon hd){
		dshd.remove(hd);
		hddm = new HoaDonDataModel(dshd);
		DAO.xoahoadon(hd);
		return null;
	}
	public String xoahoadonchitiet(HoaDon hd){
		dshd.remove(hd);
		hddm = new HoaDonDataModel(dshd);
		DAO.xoahoadon(hd);
		return "quanlyhoadon";
	}
	public String chitiet(HoaDon hd){
		List<SanPham> lsp = new ArrayList<SanPham>(hd.getDssp());
		spdm = new SanPhamDataModel(lsp);
		selectedHoaDon = hd;
		return "chitiethoadon";
	}
	public Integer soluong(SanPham sp){
		return DAO.soluong(sp.getTen(), selectedHoaDon.getMahoadon());
	}
	public BigInteger thanhtien(SanPham sp){
		return DAO.thanhtien(sp.getTen(), selectedHoaDon.getMahoadon());
	}
}
